package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.entity.GatewayAccessLogs;
import com.tebiecloud.base.server.service.GatewayAccessLogsService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 开放网关-访问日志 前端控制器
 * @author tebie
 * @date 2019-08-08
 */
@RestController
@RequestMapping("gatewayaccesslogs")
public class GatewayAccessLogsController {

    @Autowired
    private GatewayAccessLogsService gatewayAccessLogsService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ResultBody<IPage<GatewayAccessLogs>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        GatewayAccessLogs query = pageParams.mapToObject(GatewayAccessLogs.class);
        QueryWrapper<GatewayAccessLogs> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(gatewayAccessLogsService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ResultBody<GatewayAccessLogs> get(@RequestParam("id") Long id) {
        GatewayAccessLogs entity = gatewayAccessLogsService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param path 访问路径,
     * @param params 请求数据,
     * @param headers 请求头,
     * @param ip 请求IP,
     * @param httpStatus 响应状态,
     * @param method 请求类型,
     * @param requestTime 访问时间,
     * @param responseTime 响应时间,
     * @param useTime 耗时,
     * @param userAgent 用户代理,
     * @param region 区域,
     * @param authentication 认证信息,
     * @param serviceId 服务名,
     * @param error 错误信息
     * @return
     */
    @PostMapping("/add")
    public ResultBody add(
        @RequestParam(value = "path", required = false) String path,
        @RequestParam(value = "params", required = false) String params,
        @RequestParam(value = "headers", required = false) String headers,
        @RequestParam(value = "ip", required = false) String ip,
        @RequestParam(value = "httpStatus", required = false) String httpStatus,
        @RequestParam(value = "method", required = false) String method,
        @RequestParam(value = "requestTime", required = false) Date requestTime,
        @RequestParam(value = "responseTime", required = false) Date responseTime,
        @RequestParam(value = "useTime", required = false) Long useTime,
        @RequestParam(value = "userAgent", required = false) String userAgent,
        @RequestParam(value = "region", required = false) String region,
        @RequestParam(value = "authentication", required = false) String authentication,
        @RequestParam(value = "serviceId", required = false) String serviceId,
        @RequestParam(value = "error", required = false) String error
    ) {
        GatewayAccessLogs entity = new GatewayAccessLogs();
        entity.setPath(path);
        entity.setParams(params);
        entity.setHeaders(headers);
        entity.setIp(ip);
        entity.setHttpStatus(httpStatus);
        entity.setMethod(method);
        entity.setRequestTime(requestTime);
        entity.setResponseTime(responseTime);
        entity.setUseTime(useTime);
        entity.setUserAgent(userAgent);
        entity.setRegion(region);
        entity.setAuthentication(authentication);
        entity.setServiceId(serviceId);
        entity.setError(error);
        gatewayAccessLogsService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param path 访问路径
     * @param params 请求数据
     * @param headers 请求头
     * @param ip 请求IP
     * @param httpStatus 响应状态
     * @param method 请求类型
     * @param requestTime 访问时间
     * @param responseTime 响应时间
     * @param useTime 耗时
     * @param userAgent 用户代理
     * @param region 区域
     * @param authentication 认证信息
     * @param serviceId 服务名
     * @param error 错误信息
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(
            @RequestParam(value = "accessId", required = false) Long accessId,
            @RequestParam(value = "path", required = false) String path,
            @RequestParam(value = "params", required = false) String params,
            @RequestParam(value = "headers", required = false) String headers,
            @RequestParam(value = "ip", required = false) String ip,
            @RequestParam(value = "httpStatus", required = false) String httpStatus,
            @RequestParam(value = "method", required = false) String method,
            @RequestParam(value = "requestTime", required = false) Date requestTime,
            @RequestParam(value = "responseTime", required = false) Date responseTime,
            @RequestParam(value = "useTime", required = false) Long useTime,
            @RequestParam(value = "userAgent", required = false) String userAgent,
            @RequestParam(value = "region", required = false) String region,
            @RequestParam(value = "authentication", required = false) String authentication,
            @RequestParam(value = "serviceId", required = false) String serviceId,
            @RequestParam(value = "error", required = false) String error
    ) {
        GatewayAccessLogs entity = new GatewayAccessLogs();
        entity.setAccessId(accessId);
        entity.setPath(path);
        entity.setParams(params);
        entity.setHeaders(headers);
        entity.setIp(ip);
        entity.setHttpStatus(httpStatus);
        entity.setMethod(method);
        entity.setRequestTime(requestTime);
        entity.setResponseTime(responseTime);
        entity.setUseTime(useTime);
        entity.setUserAgent(userAgent);
        entity.setRegion(region);
        entity.setAuthentication(authentication);
        entity.setServiceId(serviceId);
        entity.setError(error);
        gatewayAccessLogsService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            gatewayAccessLogsService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            gatewayAccessLogsService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
